package com.baidu.servlet;

import com.baidu.entity.CommentBean;
import com.baidu.entity.NewsBean;
import com.baidu.service.NewsService;
import com.baidu.utils.Page;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 创作时间：2019/9/23 14:32
 * 作者：李增强
 */
@WebServlet("/newsServlet")
public class NewsServlet extends HttpServlet {
    NewsService ns = new NewsService();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        if("list".equals(method)){
            getNewsList(req,resp);
        }else if("lookcomment".equals(method)){
            getCommentById(req,resp);
        }else if("delete".equals(method)){
            deleteNewsById(req,resp);
        }else if("toadd".equals(method)){
            toAddComment(req,resp);
        }else if("addcomment".equals(method)){
            addComment(req,resp);
        }
    }
    protected void addComment(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        NewsService ns = new NewsService();
        String newsid = req.getParameter("newsid");
        String content = req.getParameter("content");
        String author = req.getParameter("author");
        ns.addComment(newsid,content,author);
        getCommentById(req,resp);
    }
    protected void toAddComment(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        NewsService ns = new NewsService();
        String newsid = req.getParameter("newsid");
        req.setAttribute("newsid", newsid);
        req.getRequestDispatcher("/WEB-INF/pages/news/news_add.jsp").forward(req, resp);
    }
    protected void deleteNewsById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        NewsService ns = new NewsService();
        String newsid = req.getParameter("newsid");
        boolean flag = ns.deleteNewsById(newsid);
        String msg ="删除失败";
        if(flag){
            msg="删除成功";
        }
        resp.getWriter().write(msg);
    }

    protected void getCommentById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        NewsService ns = new NewsService();
        String newsid = req.getParameter("newsid");
        List<CommentBean> list = ns.getCommentById(newsid);
        req.setAttribute("list", list);
        req.getRequestDispatcher("/WEB-INF/pages/news/news_comment.jsp").forward(req, resp);
    }
    protected void getNewsList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        NewsService ns = new NewsService();

        String yema = req.getParameter("yema");
        String title = req.getParameter("title");
        Integer count = ns.getNewsCount(title);
        Page page = new Page(yema, count, "3");
        List<NewsBean> list = ns.getNewsList(title,page);
        req.setAttribute("list", list);
        req.setAttribute("title", title);
        req.setAttribute("page", page);
        req.getRequestDispatcher("/WEB-INF/pages/news/news_list.jsp").forward(req, resp);
    }
}